package com.example.demo.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.example.demo.vo.AnalyseResultVO;

public class Analyzer {
    private String path;
    private boolean success = false;
    private String[] peopleNamesArr;
    private String[] verbsArr;
    private String[] nounsArr;
    private String[] adjsArr;
    private String[] addressesArr;
    private String[] datesArr;
    private String[] courtsArr;
    private String[] nationsArr;
    private String[] sexArr;
    private String[] cousesArr;


    public Analyzer(String path) {
        this.path = path;

        Process process;
        String[] args = new String[]{"python", "src\\main\\python\\seg.py",  path};
        try {
            process = Runtime.getRuntime().exec(args);

            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            
            // 第一行：人名集合
            this.peopleNamesArr = in.readLine().replace("{", "").replace("}", "").replace("'", "").replace(" ", "").split(",");
            
            // 第二行：动词集合(v,nums)
            this.verbsArr = kVPairArrStr2StrArr(in.readLine());

            // 第三行：名词集合(n,nums)
            this.nounsArr = kVPairArrStr2StrArr(in.readLine());

            // 第四行：形容词集合(adj,nums)
            this.adjsArr = kVPairArrStr2StrArr(in.readLine());

            // 第五行：时间
            this.datesArr = in.readLine().replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("'", "").replace(" ", "").split(",");

            // 第六行：地址
            this.addressesArr = in.readLine().replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("'", "").replace(" ", "").split(",");

            // 第七行：法院
            this.courtsArr = in.readLine().replace("[", "").replace("]", "").replace("'", "").replace(" ", "").split(",");

            // 第八行：民族
            this.nationsArr = in.readLine().replace("[", "").replace("]", "").replace("'", "").replace(" ", "").split(",");

            // 第九第十行：性别
            sexArr = new String[2];
            if (in.readLine().equals("1")) {
                if (in.readLine().equals("1")) {
                    sexArr = new String[]{"男", "女"};
                } else {
                    sexArr = new String[]{"男"};
                }
            } else {
                if (in.readLine().equals("1")) {
                    sexArr = new String[]{"女"};
                } else {
                    sexArr = new String[0];
                }
            }
            

            // 第十一行：可能的案由
            this.cousesArr = in.readLine().replace("[", "").replace("]", "").replace("'", "").replace(" ", "").split(",");

            in.close();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            this.success = false;
            return;
        }
        this.success = true;
    }

    public String[] getPeopleNames() { 
        if (this.success) return this.peopleNamesArr; 
        return null;
    }

    public String[] getVerbs() { 
        if (this.success) return this.verbsArr; 
        return null;
    }

    public String[] getNouns() {
        if (this.success) return this.nounsArr;
        return null;
    }

    public String[] getAdjs() {
        if (this.success) return this.adjsArr;
        return null;
    }

    public String[] getAddresses() { 
        if (this.success) return this.addressesArr; 
        return null;
    }

    public String[] getDates() { 
        if (this.success) return this.datesArr; 
        return null;
    }

    public String[] getCourts() { 
        if (this.success) return this.courtsArr; 
        return null;
    }

    public String[] getNations() { 
        if (this.success) return this.nationsArr; 
        return null;
    }

    public boolean isSuccess() { 
        return this.success; 
    }

    public String[] getSex() {
        if (this.success) return this.sexArr;
        return null;
    }

    public String[] getCouses() {
        if (this.success) return this.cousesArr;
        return null;
    }

    private String[] kVPairArrStr2StrArr (String input) {
        String[] tempArr = input.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("'", "").replace(" ", "").split(",");
        String[] res = new String[tempArr.length / 2];
        for (int i = 0; i < res.length; i++) {
            res[i] = tempArr[2 * i];
        }
        return res;
    }

    public AnalyseResultVO getResultVO() {
        AnalyseResultVO res = new AnalyseResultVO();

        res.setDates(this.datesArr);

        int index = 0;
        String[] nouns = new String[this.nounsArr.length + this.datesArr.length + this.peopleNamesArr.length + this.addressesArr.length + this.courtsArr.length + this.sexArr.length + this.cousesArr.length + this.nationsArr.length];
        for (int i = 0; i < this.peopleNamesArr.length; i++) {
            nouns[index] = this.peopleNamesArr[i];
            index++;
        }
        for (int i = 0; i < this.addressesArr.length; i++) {
            nouns[index] = this.addressesArr[i];
            index++;
        }
        for (int i = 0; i < this.courtsArr.length; i++) {
            nouns[index] = this.courtsArr[i];
            index++;
        }
        for (int i = 0; i < this.sexArr.length; i++) {
            nouns[index] = this.sexArr[i];
            index++;
        }
        for (int i = 0; i < this.cousesArr.length; i++) {
            nouns[index] = this.cousesArr[i];
            index++;
        }
        for (int i = 0; i < this.nationsArr.length; i++) {
            nouns[index] = this.nationsArr[i];
            index++;
        }
        for (int i = 0; i < this.nounsArr.length; i++) {
            nouns[index] = this.nounsArr[i];
            index++;
        }
        res.setNouns(nouns);

        res.setVerbs(this.verbsArr);

        res.setAdjs(this.adjsArr);

        return res;
    }

}
